<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;生产管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;进度管理</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
      <el-divider content-position="left">进度查询</el-divider>
      <el-form ref="form">
        <el-row style="vertical-align:bottom;">
          <el-col :span="1">&nbsp;</el-col>
          <el-col :span="8" style="display:inline-block;">
            <el-form-item>
              <span style="font-size: 18px;float: left">搜索进度&nbsp;</span>
              <el-input v-model="inputName" placeholder="请输入进度名称" style="width: 500px"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table :data="progress.slice((currentPage-1)*pageSize,currentPage*pageSize)" stripe style="width: 100%">
        <el-table-column prop="projectId" label="项目编号" width="200px"></el-table-column>
        <el-table-column prop="projectName" label="项目名称" width="200px"></el-table-column>
        <el-table-column prop="name" label="负责人" width="200px"></el-table-column>
        <el-table-column prop="cell" label="负责人电话" width="200px"></el-table-column>
        <el-table-column prop="email" label="电子邮件" width="200px"></el-table-column>
        <el-table-column prop="expectedTime" label="预期时间" width="200px"></el-table-column>
        <el-table-column prop="start" label="项目开始时间" width="200px"></el-table-column>
        <el-table-column prop="end" label="项目结束时间" width="200px"></el-table-column>
        <el-table-column prop="checkOver" label="是否结束" width="200px"></el-table-column>
        <el-table-column prop="cost" label="费用" width="200px"></el-table-column>
        <el-table-column label="操作" width="200px" fixed="right">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="primary"
                circle
                v-if="display==0"
                @click="handleEdit (scope.row)"><i class="el-icon-edit" style="color: white"></i>
            </el-button>
            <el-button
                size="mini"
                type="danger"
                circle
                @click="openDeleteConfirm(scope.row)"><i class="el-icon-delete" style="color: white"></i>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column></el-table-column>
      </el-table>
      <div style="margin-top:15px;">
        <el-pagination align='center' @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="currentPage"
                       :page-sizes="[1,5,10,20]"
                       :page-size="pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="progress.length">
        </el-pagination>
      </div>
    </div>
    <!--修改进度弹出的对话框-->
    <el-dialog title="编辑项目进度" :visible.sync="dialogFormVisible" append-to-body>
      <el-form :model="progressForm" :rules="rules" :ref="progressForm">
        <el-form-item label="项目编号" :label-width="formLabelWidth" prop="projectId">
          <el-input v-model="progressForm.projectId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="项目名称" :label-width="formLabelWidth" prop="projectName">
          <el-input v-model="progressForm.projectName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="负责人" :label-width="formLabelWidth" prop="name">
          <el-input v-model="progressForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="负责人电话" :label-width="formLabelWidth" prop="cell">
          <el-input v-model="progressForm.cell" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电子邮件" :label-width="formLabelWidth" prop="email">
          <el-input v-model="progressForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="预期时间" :label-width="formLabelWidth" prop="expectedTime">
          <el-input v-model="progressForm.expectedTime" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="项目开始时间" :label-width="formLabelWidth" prop="start">
          <el-input v-model="progressForm.start" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="项目结束时间" :label-width="formLabelWidth" prop="end">
          <el-input v-model="progressForm.end" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否结束" :label-width="formLabelWidth" prop="checkOver">
          <el-input v-model="progressForm.checkOver" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="费用" :label-width="formLabelWidth" prop="cost">
          <el-input v-model="progressForm.cost" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" @click="dialogFormVisible = false" style="color: white">取 消</el-button>
        <el-button type="primary" @click="submitAdminFrom()" style="color:white;;">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      display: 0,
      dialogFormVisible: false,
      formLabelWidth: '120px',
      inputName: '',
      progressOptions: [],
      progressN: [],
      progress: [],
      progressForm: {
        id:'',
        projectId: '',
        projectName: '',
        name: '',
        cell: '',
        email: '',
        expectedTime: '',
        start: '',
        end: '',
        checkOver: '',
        cost: '',
      },
      currentPage: 1,
      pageSize: 10,
      rules:  {
        projectId: [
          {required: true, message: "请输入项目编号", trigger: 'blur'},
          {pattern: /[0-9]*/, message: "请输入中文", trigger: 'blur'}
        ],
        projectName: [
          {required: true, message: "请输入项目名称", trigger: 'blur'},
          {
            pattern: /([\u4E00-\u9FA5])+([\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c])*/,
            message: "请输入中文",
            trigger: 'blur'
          }
        ],
        name: [
          {required: true, message: "请输入负责人", trigger: 'blur'},
          {pattern: /([\u4E00-\u9FA5])+/, message: "请输入中文", trigger: 'blur'},
        ],
        cell: [
          {required: true, message: "请输入负责人电话", trigger: 'blur'},
          {
            pattern: /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/,
            message: "请输入正确的电话格式",
            trigger: 'blur'
          }
        ],
        email: [
          {required: true, message: "请输入电子邮件", trigger: 'blur'},
          {pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, message: "请输入正确邮箱格式", trigger: 'blur'}
        ],
        expectedTime:
            [
              {required: true, message: "请输入预期时间", trigger: 'blur'},
              {pattern:  /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/, message: "请输入正确日期格式", trigger: 'blur'}
            ],
        start:
            [
              {required: true, message: "请输入项目开始时间", trigger: 'blur'},
              {pattern:  /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/, message: "请输入正确日期格式", trigger: 'blur'}
            ],
        end:
            [
              {required: true, message: "请输入项目结束时间", trigger: 'blur'},
              {pattern:  /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/, message: "请输入正确日期格式", trigger: 'blur'}
            ],
        checkOver:
            [
              {required: true, message: "请选择是否结束", trigger: 'blur'},
            ],
        cost:
            [
              {required: true, message: "请输入费用", trigger: 'blur'},
              {pattern: /(^[1-9][0-9]{0,7}$)|(^((0\.0[1-9]$)|(^0\.[1-9]\d?)$)|(^[1-9][0-9]{0,7}\.\d{1,2})$)/, message: "请输入正确价格格式", trigger: 'blur'}
            ]
      },
    }
  },
  methods: {
    /*加载表格*/
    loadProgressNList() {
      let url = 'http://localhost:9080/produceProgress';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.progressOptions = responseBody.data;
          this.progress = responseBody.data;
          this.progressN = this.progress;
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    /*查询进度明细*/
    searchName(keywords) {
      return this.progress.filter(item => {
        if (item.projectName.includes(keywords)) {
          return item
        }
      })
    },
    /*弹出修改窗口*/
    handleEdit(progress) {
      let url = 'http://localhost:9080/produceProgress/' + progress.id;
      console.log('id=' + progress.id);
      console.log('url' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        console.log('state=' + responseBody.state);
        if (responseBody.state == 20000) {
          this.dialogFormVisible = true;
          this.progressForm = responseBody.data;
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadProgressNList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      })
    },
    /*修改进度的窗口*/
    submitAdminFrom() {
      let url = 'http://localhost:9080/produceProgress/' + this.progressForm.id + '/update';
      console.log('url' + url);
      let formData = this.qs.stringify(this.progressForm);
      console.log('formData=' + formData)

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            type: 'success',
            message: '修改项目进度成功!'
          });
          this.dialogFormVisible = false;
          this.loadProgressNList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadProgressNList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      })
    },
    /*弹出进度删除窗口*/
    openDeleteConfirm(progress) {
      let title = '提示';
      let message = '此操作将永久删除【' + progress.projectName + '】项目进度, 是否继续?';
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(progress);
      }).catch(() => {
      });
    },
    /*删除进度窗口*/
    handleDelete(progress) {
      let url = 'http://localhost:9080/produceProgress/' + progress.id + '/delete';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除项目进度【' + progress.projectName + '】成功！',
            type: 'success'
          });
          this.loadProgressNList();
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadProgressNList();
            }
          });
        }
      });
    },
    /*表格*/
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
  },
  mounted() {
    this.loadProgressNList();
  },
  watch: {
    inputName: function (val) {
      if (val == '') {
        this.progress = this.progressN
      } else {
        this.progress = this.searchName(val)
      }
    }
  }
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

.progress_formSubmit_search {
  width: 200px;
  float: left;
  right: 10px;
  background-color: white;
}

</style>